package kr.ac.skhu.m.controller.university;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.ac.skhu.m.data.InfoValue;
import kr.ac.skhu.m.value.ResponseSchoolCalendarValue;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.HTMLElements;
import net.htmlparser.jericho.Source;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/*
 * 학사일정
 */

public class SchoolCalendarC extends AbstractController {
	
	private String successView;
	
	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView mav = new ModelAndView(successView);
	
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		
		String urlStr = "http://www.skhu.ac.kr/calendar/calendar_list_1.aspx?strYear=" + year + "&strMonth=" + month;
		URL url = new URL(urlStr);
		Source source = new Source(url);
		
		source.fullSequentialParse();
		
		List<InfoValue> thisMonth = new ArrayList<InfoValue>();
		
		Element top = source.getAllElementsByClass("top").get(0);
		Element topTbody = top.getAllElements(HTMLElementName.TBODY).get(0);
		List<Element> topTr = topTbody.getAllElements(HTMLElementName.TR);
		int topTrSize = topTr.size();
		
		for(int i=1; i<topTrSize; i++){
			InfoValue infoValue = new InfoValue();
			
			String day = topTr.get(i).getAllElements(HTMLElementName.TD).get(0).getTextExtractor().toString();
			String txt = topTr.get(i).getAllElements(HTMLElementName.TD).get(1).getTextExtractor().toString();
			
			infoValue.setDay(day);
			infoValue.setTxt(txt);
			
			thisMonth.add(infoValue);
		}
		
		mav.addObject("thisMonth", thisMonth);
		mav.addObject("year", year);
		mav.addObject("month", month);
		
		List<Element> info = source.getAllElementsByClass("info");
		int infoSize = info.size();
		
		for(int i=1; i<infoSize; i++){
			
			List<InfoValue> infoValue = new ArrayList<InfoValue>();
			
			Element infoUl = info.get(i).getAllElements(HTMLElementName.UL).get(0);
			List<Element> li = infoUl.getAllElements(HTMLElementName.LI);
			int liSize = li.size();
			
			for(int j=0; j<liSize; j++){
				InfoValue value = new InfoValue();
				
				List<Element> span = li.get(j).getAllElements(HTMLElementName.SPAN);
				String spanDay = span.get(0).getTextExtractor().toString();
				String spanTxt = span.get(1).getTextExtractor().toString();
				
				value.setDay(spanDay);
				value.setTxt(spanTxt);
				
				infoValue.add(value);
			}
			
			mav.addObject("month" + i, infoValue);
			
		}
		
		
		return mav;
	}
	
}
